﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using System.Text;

namespace ExaminationSys
{
    public partial class StudyCenter : BasePage
    {
        public string LoginCount { get; set; }
        public string LastLoginTime { get; set; }
        public string LoginName { get; set; }

        private static readonly LoginLogService Proxy = ObjectFactory<LoginLogService>.Create();
        private static readonly SectionLinkService ProxySectionlink = ObjectFactory<SectionLinkService>.Create();
        private static readonly SectionDownloadService ProxySectionDownload = ObjectFactory<SectionDownloadService>.Create();
        private static readonly SectionInfoService ProxySectionInfo = ObjectFactory<SectionInfoService>.Create();
        private static readonly LoginLogService ProxyLogin = ObjectFactory<LoginLogService>.Create();
        private static readonly PaperInfoService ProxyPaperInfoService = ObjectFactory<PaperInfoService>.Create();
        private static readonly StudentTestHistoryService ProxyPaperInfoStudentTestHistory = ObjectFactory<StudentTestHistoryService>.Create();
        private static readonly SectionInformationService ProxySectionInformation = ObjectFactory<SectionInformationService>.Create();
        private static readonly SectionPaperService ProxySectionPaperService = ObjectFactory<SectionPaperService>.Create();
        private static readonly StudentRankService ProxyStudentRankService = ObjectFactory<StudentRankService>.Create();
        private static readonly StudentService ProxyStudentService = ObjectFactory<StudentService>.Create();

        public List<SectionInfoSearchEntity> SectionInfoSearchEntityCollection = new List<SectionInfoSearchEntity>();
        public List<SectionDownload> SectionDownloadCollection = new List<SectionDownload>();
        public List<SectionInfo> SectionInfoCollection = new List<SectionInfo>();
        public List<StudentTestHistory> StudentTestHistorCollection = new List<StudentTestHistory>();

        public LoginLog GetLoginSection(string sectionId)
        {
            if (string.IsNullOrEmpty(sectionId)) return null;
            return ProxyLogin.GetLoginSection(sectionId, UserID);
        }
      
        public List<PaperInfo> GetPaperInfoBySectionId(Guid sectionId)
        {
            return ProxyPaperInfoService.GetPaperInfoBySectionId(sectionId);
        }
        public List<SectionPaper> GetSectionPaperBySectionId(Guid sectionId)
        {
            return ProxySectionPaperService.GetPaperInfoBySectionId(sectionId);
        }
        public PaperInfo GetPaperInfo(Guid id)
        {
            return ProxyPaperInfoService.Get(id);
        }
        public bool ExistsPaperInfo(Guid sectionId)
        {
            return ProxyPaperInfoService.ExistsPaperInfo(sectionId);
        }

         public List<SectionLink> GetSectionLinkBySectionId(string sectionId)
         {
             return ProxySectionlink.GetPublishList(sectionId);
             // SectionInfoSearchEntityCollection = ProxySectionlink.GetAllSectionInfoSearchEntity();
         }

         public List<SectionDownload> GetSectionDownLoadListBySectionId(string sectionId)
         {
             return ProxySectionDownload.GetPublishList(sectionId);
         }

         public List<SectionInformation> GetSectionInformationListBySectionId(string sectionId)
         {
             return ProxySectionInformation.GetPublishList(sectionId);
         }
        public StringBuilder htmlStr = new StringBuilder();
        public StringBuilder htmlStrDownLoad = new StringBuilder();
        public StringBuilder htmlStrSectionInfo = new StringBuilder();
        public int DiligentRank = 0;
        public int ChampionRank = 0;
        public string UserID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                base.ResetSession();
                var loginInfo = Session["StudentInfo"] as Student;
                if (loginInfo != null)
                {

                    var rank = ProxyStudentRankService.GetDiligentRankBStudentId(loginInfo.ID.ToString());
                    DiligentRank = rank!=null ? rank.Rank : ProxyStudentService.GetStudentCount();
                    rank = ProxyStudentRankService.GetChampionRankBStudentId(loginInfo.ID.ToString());
                    ChampionRank = rank != null ? rank.Rank : ProxyStudentService.GetStudentCount();

                    UserID = loginInfo.UserID;
                    var log = Proxy.GetLoginLogByUserId(loginInfo.LoginID, (int) LoginType.登录);
                    LastLoginTime = log != null
                                        ? log.LoginTime.ToString("yyyy.MM.dd HH:mm")
                                        : DateTime.Now.ToString("yyyy.MM.dd HH:mm");
                    var loginCountTmp = Proxy.GetLoginCount(loginInfo.LoginID, (int) LoginType.登录);
                    LoginCount = loginCountTmp == 0 ? "1" : loginCountTmp.ToString();
                    LoginName = loginInfo.UserName;

                    //得到用户已经测试过的试卷
                    StudentTestHistorCollection =
                        ProxyPaperInfoStudentTestHistory.GetStudentPaper(loginInfo.ID.ToString());
                    SectionInfoCollection = ProxySectionInfo.GetPublishList();
                    if (SectionInfoCollection != null)
                    {
                        SectionInfoCollection.Sort((x, y) => x.OrderIndex.CompareTo(y.OrderIndex));
                    }
                }
                else
                {
                    Response.Redirect("Index.aspx");
                }
               
               // SectionDownloadCollection = ProxySectionDownload.GetAll();


            }

        }
    }
}